package net.bithappens.hnqe.graph;

import java.net.URI;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import net.bithappens.hnqe.IllegalParamException;

public class GraphRelation extends GraphElement implements Iterable<GraphRelationMember>{

	private String type;
	
	private Collection<EntryProperty> properties = new LinkedList<EntryProperty>();
	
	private List<GraphRelationMember> entries;

	
	
	public GraphRelation(String type, URI uri, Date lease) throws IllegalParamException {
		super(uri, lease);
		
		if(type == null || type.isEmpty()){
			throw new IllegalParamException("the relation type must not be null or empty");
		}

		this.type = type;
		this.entries = new LinkedList<GraphRelationMember>();
	}

	public GraphRelation(String type, URI uri, Date lease, List<EntryProperty> properties) throws IllegalParamException {
		super(uri, lease);
		
		if(type == null || type.isEmpty()){
			throw new IllegalParamException("the relation type must not be null or empty");
		}
		
		this.type = type;
		this.properties = properties;
		this.entries = new LinkedList<GraphRelationMember>();
	}

	@Override
	public String getTypeId() {
		return type;
	}
	
	public Collection<EntryProperty> getProperties() {
		return properties;
	}

	public void setProperties(Collection<EntryProperty> properties) {
		this.properties = properties;
	}

	public void addProperty(EntryProperty properties) {
		this.properties.add(properties);
	}
	
	public void addProperty(String name, EntryValue value) throws IllegalParamException {
		this.properties.add(new EntryProperty(name, value));
	}
	
	public void addEntry(GraphRelationMember member){
		entries.add(member);
	}

	public void setEntries(List<GraphRelationMember> entries) {
		this.entries = entries;
	}

	public List<GraphRelationMember> getEntries(){
		return entries;
	}

	public int arity(){
		return entries.size();
	}
	
	@Override
	public Iterator<GraphRelationMember> iterator() {
		return entries.iterator();
	}


	@Override
	public String toString() {
		String propertiesAsString = "";
		for(EntryProperty property : properties){
			propertiesAsString += property +", ";
		}
		
		String entriesAsString = "";
		for(Object entry: entries){
			entriesAsString += entry+", ";
		}
		
		return "GraphRelation [uri="+getUri()+" name=" + type + " {"+propertiesAsString+"} | " + entriesAsString + "]";
	}


	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((getUri() == null) ? 0 : getUri().hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GraphRelation other = (GraphRelation) obj;
		if (getUri() == null) {
			if (other.getUri() != null)
				return false;
		} else if (!getUri().equals(other.getUri()))
			return false;
		return true;
	}

}
